package com.teng.mylibrary.widget

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.teng.mylibrary.R

/**
 * Description:  时间选择器
 * Author:       liuteng
 * CreateDate:   2021/8/24 10:04
 * UpdateRemark: 更新说明
 */
class TimePickerDialog(
    private val mContext: Context,
    private val timePickerDialogInterface: TimePickerDialogInterface
) {
    private var mAlertDialog: AlertDialog.Builder? = null
    var hour = 0
        private set
    var minute = 0
        private set
    private var mTimePicker: TimePicker? = null
    private var mDatePicker: DatePicker? = null
    private var mTag = 0
    var year = 0
        private set
    var day = 0
        private set
    private var mMonth = 0

    /**
     * 初始化DatePicker
     * @return
     */
    private fun initDatePicker(): View {
        val inflate: View = LayoutInflater.from(mContext).inflate(
            R.layout.datepicker_layout, null
        )
        mDatePicker = inflate
            .findViewById<View>(R.id.datePicker) as DatePicker
        resizePikcer(mDatePicker)
        return inflate
    }

    /**
     * 初始化TimePicker
     * @return
     */
    private fun initTimePicker(): View {
        val inflate: View = LayoutInflater.from(mContext).inflate(
            R.layout.timepicker_layout, null
        )
        mTimePicker = inflate
            .findViewById<View>(R.id.timePicker) as TimePicker
        mTimePicker!!.setIs24HourView(true)
        resizePikcer(mTimePicker)
        return inflate
    }

    private fun initDateAndTimePicker(): View {
        val inflate: View = LayoutInflater.from(mContext).inflate(
            R.layout.datetimepicker_layout, null
        )
        mTimePicker = inflate
            .findViewById<View>(R.id.dateAndTimePicker_timePicker) as TimePicker
        mDatePicker = inflate
            .findViewById<View>(R.id.dateAndTimePicker_datePicker) as DatePicker
        mTimePicker!!.setIs24HourView(true)
        resizePikcer(mTimePicker)
        resizePikcer(mDatePicker)
        return inflate
    }

    /**
     * 创建dialog
     * @param view
     */
    private fun initDialog(view: View) {
        mAlertDialog?.setPositiveButton("确定",
            object : DialogInterface.OnClickListener {
                override fun onClick(dialog: DialogInterface, which: Int) {
                    dialog.dismiss()
                    if (mTag == 0) {
                        timePickerValue
                    } else if (mTag == 1) {
                        datePickerValue
                    } else if (mTag == 2) {
                        datePickerValue
                        timePickerValue
                    }
                    timePickerDialogInterface.positiveListener()
                }
            })
        mAlertDialog?.setNegativeButton("取消"
        ) { dialog, which -> //                    timePickerDialogInterface.negativeListener()
            dialog.dismiss()
        }
        mAlertDialog?.setView(view)
    }

    /**
     * 显示时间选择器
     */
    fun showTimePickerDialog() {
        mTag = 0
        val view: View = initTimePicker()
        mAlertDialog = AlertDialog.Builder(mContext)
//        mAlertDialog!!.setTitle("选择时间")
        initDialog(view)
        mAlertDialog!!.show()
    }

    /**
     * 显示日期选择器
     */
    fun showDatePickerDialog() {
        mTag = 1
        val view: View = initDatePicker()
        mAlertDialog = AlertDialog.Builder(mContext)
//        mAlertDialog!!.setTitle("选择时间")
        initDialog(view)
        mAlertDialog!!.show()
    }

    /**
     * 显示日期时间选择器
     */
    fun showDateAndTimePickerDialog() {
        mTag = 2
        val view: View = initDateAndTimePicker()
        mAlertDialog = AlertDialog.Builder(mContext)
        //        mAlertDialog.setTitle("选择时间");
        initDialog(view)
        mAlertDialog!!.show()
    }

    /*
     * 调整numberpicker大小
     */
    private fun resizeNumberPicker(np: NumberPicker?) {
        val params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(
            120,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        params.setMargins(10, 0, 10, 0)
        if (np != null) {
            np.layoutParams = params
        }
    }

    /**
     * 调整FrameLayout大小
     * @param tp
     */
    private fun resizePikcer(tp: FrameLayout?) {
        val npList: List<NumberPicker?> = findNumberPicker(tp)
        for (np in npList) {
            resizeNumberPicker(np)
        }
    }

    /**
     * 得到viewGroup里面的numberpicker组件
     * @param viewGroup
     * @return
     */
    private fun findNumberPicker(viewGroup: ViewGroup?): List<NumberPicker?> {
        val npList: ArrayList<NumberPicker?> = ArrayList<NumberPicker?>()
        var child: View? = null
        if (null != viewGroup) {
            for (i in 0 until viewGroup.childCount) {
                child = viewGroup.getChildAt(i)
                if (child is NumberPicker) {
                    npList.add(child as NumberPicker?)
                } else if (child is LinearLayout) {
                    val result: List<NumberPicker?> = findNumberPicker(child as ViewGroup?)
                    if (result.isNotEmpty()) {
                        return result
                    }
                }
            }
        }
        return npList
    }

    //返回的时间是0-11
    val month: Int
        get() =//返回的时间是0-11
            mMonth + 1

    /**
     * 获取日期选择的值
     */
    private val datePickerValue: Unit
        private get() {
            year = mDatePicker?.year!!
            mMonth = mDatePicker?.month!!
            day = mDatePicker?.dayOfMonth!!
        }// api23这两个方法过时
    // timePicker.getHour();
    // timePicker.getMinute();
    /**
     * 获取时间选择的值
     */
    private val timePickerValue: Unit
        private get() {
            // api23这两个方法过时
            hour = mTimePicker?.currentHour!!  // timePicker.getHour();
            minute = mTimePicker?.currentMinute!! // timePicker.getMinute();
        }

    interface TimePickerDialogInterface {
        fun positiveListener()
//        fun negativeListener()
    }
}